//
//  ListViewController.m
//  GiftSay
//
//  Created by QQn1993 on 16/1/16.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "ListViewController.h"
#import "LongTableViewCell.h"
#import "DataBaseHandle.h"
#import "HomeNewModel.h"
#import "collect.h"


@interface ListViewController ()<UITableViewDataSource,UITableViewDelegate,LongTableViewCellDelegate>

@property (nonatomic,strong) UITableView *table;
@property (nonatomic,strong) NSMutableArray *array;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray arrayWithCapacity:0];
    self.view.backgroundColor = [UIColor whiteColor];
    if ([self.usein isEqualToString:@"post"]) {
        self.title = @"攻略列表";
    }
    else{
        self.title = @"礼物列表";
    }
    
    [self createdTable];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    NSLog(@"%@",self.usein);
    [[DataBaseHandle shareDataBase] openDataBase];
    if (self.array.count > 0) {
        [self.array removeAllObjects];
    }
    self.array = [NSMutableArray arrayWithArray:[[DataBaseHandle shareDataBase] selectMealsArray:self.usein]];
    
    [self.table reloadData];
//    NSLog(@"%@", self.table);
//    NSLog(@"%@",self.array);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES){
        self.table.backgroundColor = kNightModeColor;
    }
    else{
        self.table.backgroundColor = [UIColor whiteColor];
    }
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[DataBaseHandle shareDataBase] closeDatabase];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES){
        self.table.backgroundColor = kNightModeColor;
        self.navigationController.navigationBar.barTintColor = kNightModeNavigationColor;
    }
    
    else{
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:248 / 255.0 green:78 / 255.0 blue:78 / 255.0 alpha:1];
        self.table.backgroundColor = [UIColor whiteColor];
    }

}

- (void)createdTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStyleGrouped];
    self.table.backgroundColor = [UIColor whiteColor];
    [self.table registerClass:[LongTableViewCell class] forCellReuseIdentifier:@"LongTableViewCell"];
    
    self.table.delegate = self;
    self.table.dataSource = self;
    
    [self.view addSubview:self.table];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kWidth / 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LongTableViewCell" forIndexPath:indexPath];
    collect *col = self.array[indexPath.row];
    HomeNewModel *home = [[HomeNewModel alloc] init];
    home.title = col.name;
    home.cover_image_url = col.img;
    home.url = col.ID;
    cell.model = home;
    cell.delegate = self;
    cell.likeCount.hidden = YES;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController *web = [[WebViewController alloc] init];
    web.url = [self.array[indexPath.row] ID];
    web.type = self.usein;
    
    [self.navigationController pushViewController:web animated:YES];
}

- (void)deleteing:(NSString *)title
{
    NSLog(@"%ld",self.array.count);
//    [self.table reloadData];
    NSMutableArray *temp = [NSMutableArray arrayWithCapacity:0];
    for (collect *c  in self.array) {
        NSLog(@"%@   %@",c.name ,title);
        if ([c.name isEqualToString:title]) {
            NSIndexPath *indext = [NSIndexPath indexPathForItem:[self.array indexOfObject:c] inSection:0];
            [temp addObject:indext];
        }
    }
    
    for (NSIndexPath *indext in temp) {
        [self.array removeObjectAtIndex:indext.row];
    }
    
    [self.table deleteRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationBottom];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
